package hd.util.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import hd.util.ColorUtil;
import hd.util.ScreenHelper;

/**
 * 菜单 更多按钮（绘画三个点）
 * <p/>
 * Created by haidy on 14/12/6.
 */
public class MenuMoreView extends View {

    private boolean pressed = false;
    private int color = Color.WHITE;
    private Paint normalPaint;
    private Paint pressPaint;
    private Paint paint;
    private int absPaintLocation;
    private int circleRadius;

    public MenuMoreView(Context context) {
        this(context, null);
    }

    public MenuMoreView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MenuMoreView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        normalPaint = new Paint();
        normalPaint.setAntiAlias(true);
        normalPaint.setColor(color);
        normalPaint.setStyle(Paint.Style.FILL);

        pressPaint = new Paint(normalPaint);
        pressPaint.setColor(ColorUtil.alphaColor(0x80, color));

        //绘制圆的半径
        circleRadius = ScreenHelper.dp2px(getContext(), 2);

        //初始化另外两个圆的位置,中点上面10dp和中点下面10dp
        absPaintLocation = ScreenHelper.dp2px(getContext(), 10);

    }

    /**
     * 设置显示的颜色
     *
     * @param color 颜色
     */
    public void setColor(int color) {
        this.color = color;
        normalPaint.setColor(color);
        pressPaint.setColor(ColorUtil.alphaColor(0x80, color));
        invalidate();
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                pressed = true;
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                if (pressed) {
                    if (event.getX() > getWidth() || event.getY() > getHeight()
                            || event.getX() < 0 || event.getY() < 0) {
                        pressed = false;
                        postInvalidateDelayed(100);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                pressed = false;
                postInvalidateDelayed(100);
                break;
        }
        return true;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(getWidth() / 2, getHeight() / 2);
        if (pressed) {
            paint = pressPaint;
        } else {
            paint = normalPaint;
        }
        //先绘制中间的圆
        canvas.drawCircle(0, 0, circleRadius, paint);
        //绘制上面的圆
        canvas.drawCircle(-absPaintLocation, 0, circleRadius, paint);
        //绘制下面的圆
        canvas.drawCircle(absPaintLocation, 0, circleRadius, paint);
    }
}
